package com.anyonavinfo.cpaddemo.view.musicplayerview;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.media.Image;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.anyonavinfo.cpaddemo.Application;
import com.anyonavinfo.cpaddemo.MainActivity;
import com.anyonavinfo.cpaddemo.R;
import com.anyonavinfo.cpaddemo.data.musicinfo.MusicInfo;
import com.anyonavinfo.cpaddemo.data.musicinfo.source.MusicInfoRepository;
import com.anyonavinfo.cpaddemo.utils.MusicPlayerTimeUtils;
import com.anyonavinfo.cpaddemo.utils.ToastManagerUtils;
import com.anyonavinfo.cpaddemo.view.widget.circleimage.RoundImageView;

import java.util.List;

/**
 * Created by ZuiC on 2017/2/10.
 */

public class MusicPlayerView extends RelativeLayout implements MusicPlayerContract.View{

    private Context mContext;
    private ImageButton imgTurnUpVolume,imgTurnDownVolume,imgOpenMusicList,imgChangePlayMode,imgPlayPause,imgPlayPreviousMusic,imgPlayNextMusic;
    private TextView tvSongName,tvSongAuthor,tvCurProgress,tvDuration;
    private SeekBar sb_VolumeValue,sb_PlayBackProgress;
    private Button btnHideMusicList;
    private RecyclerView rvMusicList;
    private MusicPlayerContract.Presenter mPresenter;
    private MusicListAdapter musicListAdapter;
    private RelativeLayout rlMusicList;
    private SwipeRefreshLayout srlRefreshMusicList;



    public MusicPlayerView(Context context) {
        super(context);
        initView(context);
    }

    public MusicPlayerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public MusicPlayerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        this.mContext = context;
        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        View view = layoutInflater.inflate(R.layout.view_musicpalyerview, this);

        musicListAdapter = new MusicListAdapter();


        imgTurnUpVolume = (ImageButton) view.findViewById(R.id.img_turn_up_volume);
        imgTurnDownVolume = (ImageButton) view.findViewById(R.id.img_turn_down_volume);
        imgOpenMusicList = (ImageButton) view.findViewById(R.id.img_open_muisclist);
        imgChangePlayMode = (ImageButton) view.findViewById(R.id.img_changeplaymode);
        imgPlayPause = (ImageButton) view.findViewById(R.id.img_playpause);
        imgPlayPreviousMusic = (ImageButton) view.findViewById(R.id.img_previousmusic);
        imgPlayNextMusic = (ImageButton) view.findViewById(R.id.img_nextmusic);
        tvSongName = (TextView) view.findViewById(R.id.tv_song_name);
        tvSongAuthor = (TextView) view.findViewById(R.id.tv_song_author);
        tvCurProgress = (TextView) view.findViewById(R.id.tv_curprogress);
        tvDuration = (TextView) view.findViewById(R.id.tv_duration);
        sb_VolumeValue = (SeekBar) view.findViewById(R.id.sb_volume_value);
        sb_PlayBackProgress = (SeekBar) view.findViewById(R.id.sb_playbackprogress);
        btnHideMusicList = (Button) view.findViewById(R.id.btn_hidemusiclist);
        rvMusicList = (RecyclerView) view.findViewById(R.id.rv_music_list);
        rlMusicList = (RelativeLayout) view.findViewById(R.id.rl_music_list);
        srlRefreshMusicList = (SwipeRefreshLayout) view.findViewById(R.id.srl_refresh_musiclist);

        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(mContext);
        rvMusicList.setLayoutManager(layoutManager);

        setListener();

        new MusicPlayerPresenter(this, MusicInfoRepository.getInstance()).start();
    }

    private void setListener(){
        imgPlayNextMusic.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.playNextMusic();
            }
        });

        imgPlayPreviousMusic.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.playPreviousMusic();
            }
        });

        imgPlayPause.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.playStopCurMusic();
            }
        });

        imgChangePlayMode.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {mPresenter.changePlayMode();
            }
        });

        btnHideMusicList.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {rlMusicList.setVisibility(GONE);
            }
        });

        imgOpenMusicList.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {rlMusicList.setVisibility(VISIBLE);
            }
        });

        musicListAdapter.setOnItemClickListener(new MusicListAdapter.OnItemClick() {
            @Override
            public void playSelectedMusic(int selectedPosition) {
                mPresenter.playSelectedMusic(selectedPosition);
            }
            @Override
            public void deleteMusic(final int selectedPosition) {
                new AlertDialog.Builder(mContext).setCancelable(false)
                        .setTitle("提示")
                        .setMessage("是否要删除此音乐？")
                        .setPositiveButton("确定",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        mPresenter.deleteMusic(selectedPosition);
                                    }
                                })
                        .setNegativeButton("取消", null)
                        .show();
            }
        });

        sb_PlayBackProgress.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                int proPercent = seekBar.getProgress();
                mPresenter.changePlayProgress(proPercent);
            }
        });

        srlRefreshMusicList.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {mPresenter.reloadMusicInfo();
            }
        });
    }

    @Override
    public void updateMusicInfo(MusicInfo musicInfo) {
        tvCurProgress.setText("00:00");
        tvDuration.setText(MusicPlayerTimeUtils.getFormattedTime(musicInfo.getDuration()));
        tvSongName.setText(musicInfo.getTitle());
        tvSongAuthor.setText(musicInfo.getArtist());
    }

    @Override
    public void updatePlayMode(int playMode) {
        if (playMode == 0){
            imgChangePlayMode.setImageResource(R.drawable.icon_musicplayer_orderplay);
            ToastManagerUtils.getInstance().showTextToast(mContext,"顺序播放");
        }
        if (playMode == 1){
            imgChangePlayMode.setImageResource(R.drawable.icon_musicplayer_randomplay);
            ToastManagerUtils.getInstance().showTextToast(mContext,"随机播放");
        }
        if (playMode == 2){
            imgChangePlayMode.setImageResource(R.drawable.icon_musicplayer_singlecycle);
            ToastManagerUtils.getInstance().showTextToast(mContext,"单曲循环");
        }
    }

    @Override
    public void updatePlayStatus(int playStatus) {
        if (playStatus == 0){
            imgPlayPause.setSelected(false);
        }else {
            imgPlayPause.setSelected(true);
        }
    }

    @Override
    public void backToMainView() {
        ((MainActivity)mContext).hideMusicPlayerView();
    }

    @Override
    public void updateProgress(int curProgress, int percent) {
        tvCurProgress.setText(MusicPlayerTimeUtils.getFormattedTime(curProgress));
        sb_PlayBackProgress.setProgress(percent);
    }

    @Override
    public void initMusicList(List<MusicInfo> musicInfoList) {
        musicListAdapter.setMusicInfo(musicInfoList);
        rvMusicList.setAdapter(musicListAdapter);
    }

    @Override
    public void updateMusicList(List<MusicInfo> musicInfo) {
        musicListAdapter.setMusicInfo(musicInfo);
        musicListAdapter.notifyDataSetChanged();
        srlRefreshMusicList.setRefreshing(false);
    }


    @Override
    public void setPresenter(MusicPlayerContract.Presenter presenter) {
        mPresenter = presenter;
    }

    public void onBackPressed(){
        mPresenter.onBackPressed();
    }

    public void onRedisplay(){
        mPresenter.refreshView();
    }

}
